package com.eascs.admin.o2o.loanAccount.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.account.api.ICapitalFlowService;
import com.eascs.account.api.constants.enums.IndividualAccountType;
import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.common.util.UrlUtil;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.PaginationRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.workflow.common.vo.ModelResult;

@RestController
@RequestMapping("/raccount")
public class PersonalAccountRestController extends BaseRestController {
	private static Logger logger = LoggerFactory.getLogger(PersonalAccountRestController.class);
	@Reference
	ICapitalFlowService capitalFlowService;

	/**
	 * @Title: buildUseAccountPaginationInfo
	 * @Description:可用账户明细
	 * @author 张庆奇
	 * @param accountId
	 * @param pageIndex
	 * @return
	 * @return PaginationInfo
	 * @throws @date
	 *             2017年12月13日 下午2:01:42
	 */
	@RequestMapping("/use_account")
	public PaginationRestResult buildUseAccountPaginationInfo(String accountId, Integer pageIndex) {
		if (pageIndex == null) {
			pageIndex = 0;
		}
		String url = "/raccount/use_account";
		url = UrlUtil.appendUrl(url, "accountId", accountId);
		url = url + "&pageIndex=";
		PageResult<Object[]> pageResult = new PageResult<Object[]>();
		if (StringUtil.isNotBlank(accountId)) {
			ModelResult<PageResult<Object[]>> useResult = capitalFlowService.individualAccountPage(accountId,
					IndividualAccountType.USE, pageIndex);
			if (useResult.isSuccess() && useResult.getModel() != null) {
				pageResult = useResult.getModel();

			}
			logger.info("accountId:[{}]", accountId);
			logger.info("pageIndex:[{}]", pageIndex);
			logger.info("调用查询分页接口返回消息:[{}]", useResult.getErrMsg());
		}
		String[] titles = { "时间", "交易流水", "交易类型", "交易方向", "交易金额（元）", "当前余额（元）", "变更前金额（元）", "摘要" };
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, url);
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setBuildRowNumber(true);
		paginationInfo.setFirstRenderColumnIndex(0);
		paginationInfo.setRendTo("#use_account_result");
		return PaginationRestResult.buildPaginationRestResult(paginationInfo);
	}

	/**
	 * @Title: buildfreezeAccountPaginationInfo
	 * @Description: 冻结账户流水明细
	 * @author 张庆奇
	 * @param accountId
	 * @param pageIndex
	 * @return
	 * @return PaginationInfo
	 * @throws @date
	 *             2017年12月13日 下午2:00:46
	 */

	@RequestMapping("/freeze_account")
	public PaginationRestResult buildfreezeAccountPaginationInfo(String accountId, Integer pageIndex) {
		String url = "/raccount/freeze_account";
		url = UrlUtil.appendUrl(url, "accountId", accountId);
		url = url + "&pageIndex=";
		if (pageIndex == null) {
			pageIndex = 0;
		}
		PageResult<Object[]> pageResult = new PageResult<Object[]>();
		if (StringUtil.isNotBlank(accountId)) {
			ModelResult<PageResult<Object[]>> freezeResult = capitalFlowService.individualAccountPage(accountId,
					IndividualAccountType.FREEZE, pageIndex);
			if (freezeResult.isSuccess() && freezeResult.getModel() != null) {
				pageResult = freezeResult.getModel();

			}
			logger.info("accountId:[{}]", accountId);
			logger.info("pageIndex:[{}]", pageIndex);
			logger.info("调用查询分页接口返回消息:[{}]", freezeResult.getErrMsg());
		}
		String[] titles = { "时间", "交易流水", "交易类型", "交易方向", "交易金额（元）", "当前余额（元）", "变更前金额（元）", "摘要" };
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, url);
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setBuildRowNumber(true);
		paginationInfo.setFirstRenderColumnIndex(0);
		paginationInfo.setRendTo("#freeze_account_result");
		ViewContext.put("paginationInfo", paginationInfo);
		return PaginationRestResult.buildPaginationRestResult(paginationInfo);
	}

}
